﻿using DryIoc;
using Microsoft.Extensions.Logging;
using MyMeeting.Common;
using MyMeeting.Extensions;
using MyMeeting.Service;
using MyMeeting.ViewModels;
using MyMeeting.ViewModels.Dialogs;
using MyMeeting.Views;
using MyMeeting.Views.Dialogs;
using NLog.Extensions.Logging;
using Prism.DryIoc;
using Prism.Ioc;
using Prism.Services.Dialogs;
using Snowflake.Core;
using System;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Navigation;
using System.Windows.Threading;

namespace MyMeeting
{
    /// <summary>
    /// Interaction logic for App.xaml
    /// </summary>
    public partial class App
    {
        protected override Window CreateShell()
        {
            //ui线程未捕获的异常
            DispatcherUnhandledException += OnDispatcherUnhandledException;
            //Task线程未捕获的异常
            TaskScheduler.UnobservedTaskException += OnUnobservedTaskException;
            //多线程异常
            AppDomain.CurrentDomain.UnhandledException += OnUnhandledException;
            AppDomain.CurrentDomain.ProcessExit += CurrentDomain_ProcessExit;
            var AppId = "4a4f7be64fa1404ebda74784fe9ac381";
            PrismManager.MeetEngine.Init(AppId);
            return Container.Resolve<MainWindow>();
        }

        private void CurrentDomain_ProcessExit(object sender, EventArgs e)
        {
            PrismManager.MeetEngine.UnInit();
        }

        private ILogger _logger;
        protected override void RegisterTypes(IContainerRegistry containerRegistry)
        {
            containerRegistry.RegisterForNavigation<MeetingView, MeetingViewModel>();
            containerRegistry.RegisterForNavigation<ContactsView, ContactsViewModel>();
            containerRegistry.RegisterForNavigation<RecordView, RecordViewModel>();

            containerRegistry.RegisterDialog<OpenEmailView, OpenEmailViewModel>();
            containerRegistry.RegisterDialog<MeetDemoView, MeetDemoViewModel>();
            containerRegistry.RegisterDialog<LoginView, LoginViewModel>();
            containerRegistry.RegisterDialog<ShareScreenView, ShareScreenViewModel>();

            var factory = new NLogLoggerFactory();
            _logger = factory.CreateLogger("NLog.config");
            containerRegistry.RegisterInstance<ILogger>(_logger);
            _logger.LogDebug("日志测试");
            //containerRegistry.Register<ShengGMeetEngine>();
        }
        protected override void OnInitialized()
        {
           
            var dialog = Container.Resolve<IDialogService>();

            dialog.ShowDialog("LoginView", callback =>
            {
                if (callback.Result != ButtonResult.OK)
                {
                    Environment.Exit(0);
                    return;
                }
                var config = App.Current.MainWindow.DataContext as IConfigService;
                base.OnInitialized();

                if (config != null)
                {
                    config.Loaded();

                }
            });
        }
        private void OnDispatcherUnhandledException(object sender, DispatcherUnhandledExceptionEventArgs e)
        {
            _logger.LogCritical($"{e.Exception.StackTrace},{e.Exception.Message}");
        }

        private void OnUnobservedTaskException(object? sender, UnobservedTaskExceptionEventArgs e)
        {
            _logger.LogCritical($"{e.Exception.StackTrace},{e.Exception.Message}");
        }

        private void OnUnhandledException(object sender, UnhandledExceptionEventArgs e)
        {
            Exception ex = e.ExceptionObject as Exception;
            _logger.LogCritical($"{ex.StackTrace},{ex.Message}");
        }
    }
}
